package jh.com.jhappbf.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.ab.util.AbAnimationUtil;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;
import jh.com.jhappbf.utils.ClearCache;

import static android.content.Context.MODE_PRIVATE;

/**
 * @author Administrator
 */
public class SlidingMenuFragment extends Fragment implements OnClickListener {
    private ImageView mSunShine;
    private ArrayList<String> mGroup;
    private ArrayList<ArrayList<String>> mChildes;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.sliding_menu_view, null);
        initView(v);
        return v;
    }

    /**
     * 初始化
     */
    private void initView(View v) {
//        ImageView mLogin = v.findViewById(R.id.sliding_menu_user_photo);
        mSunShine = v.findViewById(R.id.sliding_menu_sunshineView_img);
        TextView mLoginNameTxt = v.findViewById(R.id.sliding_menu_user_name_txt);
        TextView mUsernameTxt = v.findViewById(R.id.sliding_menu_user_point);
        ExpandableListView mListView = v.findViewById(R.id.sliding_menu_list);
        Button mBtnClear = v.findViewById(R.id.sliding_menu_clear);
        mBtnClear.setOnClickListener(this);
        Button mBtnQuit = v.findViewById(R.id.sliding_menu_quit);
        mBtnQuit.setOnClickListener(this);
        mUsernameTxt.setText(State.mAccount);
        mLoginNameTxt.setText("状态：登录");
        mGroup = new ArrayList<>();
        mGroup.add("操作");
        mGroup.add("其它");
        ArrayList<String> child1 = new ArrayList<>();
        child1.add("修改密码");
        child1.add("开启消息");
        ArrayList<String> child2 = new ArrayList<>();
        child2.add(" ");
        mChildes = new ArrayList<>();
        mChildes.add(child1);
        mChildes.add(child2);
        setUserPoint();

        MyExpandableAdapter mAdapter = new MyExpandableAdapter();
        mListView.setAdapter(mAdapter);
        for (int index = 0; index < mGroup.size(); index++) {
            mListView.expandGroup(index);
        }
    }

    /**
     * 设置太阳动画
     */
    public void setUserPoint() {
        AbAnimationUtil.playRotateAnimation(mSunShine, 2000, 10,
                Animation.RESTART);
    }

    class MyExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return mGroup.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mChildes.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return mGroup.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return mChildes.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getActivity(), R.layout.list_view_item_txt, null);
                holder = new ViewHolder();
                holder.txtGroup = convertView.findViewById(R.id.list_view_item_txt);
                holder.txtGroup.setTextColor(Color.WHITE);
                holder.txtGroup.setBackgroundColor(Color.GRAY);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txtGroup.setText(mGroup.get(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            if (groupPosition == 0 && childPosition == 1) {
                convertView = View.inflate(getActivity(), R.layout.list_view_item_toggle_btn, null);
                Switch switchBtn = convertView.findViewById(R.id.list_view_item_switch);
                SharedPreferences myPreferences = getActivity().getSharedPreferences("caption", MODE_PRIVATE);
                switchBtn.setChecked(myPreferences.getBoolean("sendMessage", false));
                switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        SharedPreferences rPreferences = getActivity().getSharedPreferences("caption", MODE_PRIVATE);
                        SharedPreferences.Editor editor = rPreferences.edit();
                        if (isChecked) {
                            editor.putBoolean("sendMessage", true);
                        } else {
                            editor.putBoolean("sendMessage", false);
                        }
                        editor.apply();
                    }
                });
            }

//            else if(groupPosition == 1 && childPosition == 0){
//                convertView = View.inflate(getActivity(), R.layout.list_view_item_notify, null);
//
//                Button save=convertView.findViewById(R.id.notify_save);
//                final View finalConvertView = convertView;
//                save.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        EditText time = finalConvertView.findViewById(R.id.notify_time);
//                        if(time.getText().toString().trim().length()>0){
//                            SharedPreferences sharedPreferences=getActivity().getSharedPreferences("caption",MODE_PRIVATE);
//                            Editor edit = sharedPreferences.edit();
//                            edit.putInt("time",Integer.parseInt(time.getText().toString()));
//                            edit.apply();
//                            Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                });
//            }


            else {
                convertView = View.inflate(getActivity(), R.layout.list_view_item_txt, null);
                TextView txt = convertView.findViewById(R.id.list_view_item_txt);
                txt.setTextColor(Color.WHITE);
                txt.setText(mChildes.get(groupPosition).get(childPosition));
                txt.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (v instanceof TextView) {
                            TextView txt = (TextView) v;
                            if ("修改密码".equals(String.valueOf(txt.getText()))) {
                                Intent i = new Intent(getActivity(), UpdatePwd.class);
                                startActivity(i);
                            }
                        }
                    }
                });
            }

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    class ViewHolder {
        public TextView txtGroup;
//        public TextView mChildName;
//        public Switch mSwitch;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sliding_menu_clear:
                ClearCache.applicationCleanCache(getActivity());
                break;

            case R.id.sliding_menu_quit:
                getActivity().finish();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);   //常规java、c#的标准退出法，返回值为0代表正常退出
                break;

            default:
                break;
        }
    }
}
